package com.jml.蚂蚁.netty.序列化;


import java.io.*;
import java.util.Date;
import java.util.UUID;

 /**
 * @author:jml
 * @date: 2019/12/25 9:46
 * @description：
 */
public class 本地硬盘序列化 {
	
	 public static void main(String[] args) {
		 String fileName="F:\\code\\msg";
		 serialize(fileName);
		 deserialize(fileName);
	 }
	
	 /**
	  * 序列化对象到文件
	  *
	  * @param fileName
	  */
	 public static void serialize(String fileName) {
		 try {
			 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
			 //序列化一个字符串到文件
			 out.writeObject("序列化的日期是：");
			 //序列化一个当前日期对象到文件
			 out.writeObject(new Date());
			 MsgEntity msgEntity = new MsgEntity(UUID.randomUUID().toString(), "牛逼!");
			 //序列化一个MsgEntity对象
			 out.writeObject(msgEntity);
			 out.close();
			
		 } catch (FileNotFoundException e) {
			 e.printStackTrace();
		 } catch (IOException e) {
			 e.printStackTrace();
		 }
	 }
	
	 /**
	  * 反序列化
	  *
	  * @param fileName
	  */
	 public static void deserialize(String fileName) {
		 try {
			 //从本地文件中读取字节 变为对象
			 ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
			 String str = (String) in.readObject();
			 //日期对象
			 Date date = (Date) in.readObject();
			 //MsgEntity对象
			 MsgEntity userInfo = (MsgEntity) in.readObject();
			 System.out.println(str);
			 System.out.println(date);
			 System.out.println(userInfo);
		 } catch (FileNotFoundException e) {
			 e.printStackTrace();
		 } catch (IOException e) {
			 e.printStackTrace();
		 } catch (ClassNotFoundException e) {
			 e.printStackTrace();
		 }
	 }
	
 }
